package com.example.opengldemo._08_3D.column;

import android.graphics.drawable.ShapeDrawable;
import android.opengl.GLES30;
import android.util.Log;
import android.view.View;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;
import com.example.opengldemo.util.ShapeFactory;


public class Columu {

    String vertexShaderPath = "d3d/vertex.sh";
    String fragShaderPath = "d3d/fragment.sh";


    public Columu(View view) {
        initProgram(view);
        initBuffer();
    }


    private void initProgram(View view) {
        String vertexShaderRes = ShaderUtil.loadFromAssetsFile(vertexShaderPath, view.getResources());
        String fragShaderRes = ShaderUtil.loadFromAssetsFile(fragShaderPath, view.getResources());
        initHandle(ShaderUtil.createProgram(vertexShaderRes, fragShaderRes));
    }



    private void initHandle(int mProgram) {
        shaderHandle = new ShaderHandle(mProgram).build();
        texture2=GLES30.glGetUniformLocation(mProgram, "sTexture2");
        texture3=GLES30.glGetUniformLocation(mProgram, "sTexture3");
    }

    public ShaderHandle shaderHandle;
    public ShaderBuffer shaderBuffer;

    private void initBuffer() {
        shaderBuffer = new ShaderBuffer();
        shaderBuffer.setVertexBuffer(getVertexArray2());
        shaderBuffer.setTexcoorBuffer(getTexureArray2());
        shaderBuffer.setColorBuffer(getColorArray());
        Log.e("Columu" , "size > " +shaderBuffer.vCount ) ;
    }



    public float[] getVertexArray() {
        int count = 360 / span + 1 + 1  ;
        float r = 0.8f;
        float[] vertex = new float[count * 3];

        vertex[0] = 0f;
        vertex[1] = 0f;
        vertex[2] = 0f;

        for (int i = 1; i < count; i++) {
            int spanTemp = (i - 1) * span;
            vertex[i * 3 + 0] = (float) (r * Math.cos(Math.toRadians(spanTemp)));
            vertex[i * 3 + 1] =  (float) (r * Math.sin(Math.toRadians(spanTemp)));
            vertex[i * 3 + 2] = 0f ;
        }
        shaderBuffer.vCount = count ;
        return vertex;
    }

    public float[] getTexcoorArray() {
        float r = 0.5f;
        float[] texcoors = new float[shaderBuffer.vCount * 2] ;
        texcoors[0]  = 0.5f ;
        texcoors[1] =  0.5f ;
        for (int i = 1; i < shaderBuffer.vCount; i++) {
            int spanTemp = (i - 1) * span;
            texcoors[i * 2 + 0] = r + (float) (r * Math.cos(Math.toRadians(spanTemp)));
            texcoors[i * 2 + 1] = r - (float) (r * Math.sin(Math.toRadians(spanTemp)));
        }
        return texcoors;
    }

    public float[] getNormalArray() {
        return null;
    }

    public float[] getColorArray(){
        return ShapeFactory.getFragDataArray(shaderBuffer.vCount);
    }
    int texture2 = 0 ;
    int texture3 = 0 ;
    public void drawSelf(int textureid , int textureid2 , int textureid3) {
        GLES30.glUseProgram(shaderHandle.getProgram());
        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_PositionHandle(),
                3,
                GLES30.GL_FLOAT,
                false,
                3 * 4,
                shaderBuffer.getVertexBuffer()
        );
        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_ColorHandle(),
                4,
                GLES30.GL_FLOAT,
                false,
                4 * 4,
                shaderBuffer.getColorBuffer()
        );

        GLES30.glVertexAttribPointer(
                shaderHandle.get_A_TexCoorsHandle(),
                2,
                GLES30.GL_FLOAT,
                false,
                2 * 4,
                shaderBuffer.getTexcoorBuffer()
        );

        GLES30.glUniformMatrix4fv(
                shaderHandle.get_U_MVPMatrixHandle(),
                1,
                false,
                shaderBuffer.getMVPMatrixArray(),
                0
        );

        GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , textureid);
        GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , textureid2);
        GLES30.glActiveTexture(GLES30.GL_TEXTURE2);
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D , textureid3);

        GLES30.glUniform1i(shaderHandle.get_U_textureHandle(), textureid);
        GLES30.glUniform1i(this.texture2, textureid2);
        GLES30.glUniform1i(this.texture3, textureid3);
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_PositionHandle());
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_ColorHandle());
        GLES30.glEnableVertexAttribArray(shaderHandle.get_A_TexCoorsHandle());

        GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0 ,  shaderBuffer.vCount);
    }
    int span = 10;
    private float[] getVertexArray2() {
        int count = 360 / span ;
        float r = 0.8f;
        float[] vertexArray = new float[count * 3 * 3 ] ;
        for (int i = 0 ; i < count ; i++){

            int spanTemp = (i) * span;
            int spanTempNext = spanTemp + span;
            vertexArray[i*9 + 0] = 0f ;
            vertexArray[i*9 + 1] = 0f ;
            vertexArray[i*9 + 2] = 0f ;

            vertexArray[i*9 + 3] = (float) (r * Math.cos(Math.toRadians(spanTemp)));
            vertexArray[i*9 + 4] =  (float) (r * Math.sin(Math.toRadians(spanTemp)));
            vertexArray[i*9 + 5] = 0f ;

            vertexArray[i*9 + 6] =  (float) (r * Math.cos(Math.toRadians(spanTempNext)));
            vertexArray[i*9 + 7] = (float) (r * Math.sin(Math.toRadians(spanTempNext)));
            vertexArray[i*9 + 8] = 0f ;

        }
        shaderBuffer.vCount = vertexArray.length/3 ;

        return vertexArray ;
    }


    public float[] getTexureArray2(){
        int count = shaderBuffer.vCount ;
        int size = count / 3 ;
        float r = 0.5f;
        float[] vertexArray = new float[count *2 ] ;
        for (int i = 0 ; i < size ; i++){

            int spanTemp = (i) * span;
            int spanTempNext = spanTemp + span;
            vertexArray[i*6 + 0] = 0.5f ;
            vertexArray[i*6 + 1] = 0.5f ;

            vertexArray[i*6 + 2] = r + (float) (r * Math.cos(Math.toRadians(spanTemp)));
            vertexArray[i*6 + 3] = r - (float) (r * Math.sin(Math.toRadians(spanTemp)));


            vertexArray[i*6 + 4] = r + (float) (r * Math.cos(Math.toRadians(spanTempNext)));
            vertexArray[i*6 + 5] = r - (float) (r * Math.sin(Math.toRadians(spanTempNext)));


        }
        return vertexArray ;
    }
}
